package zulu.editorTest;

import static bogus.core.BogusCore.egl;
import owg.util.EntityBase;
import owg.util.InstanceList;
import owg.util.data.PFieldElement;
import owg.util.data.PFieldImmutable;
import owg.util.data.PFieldOptional;
import owg.util.data.PFieldOwner;
import owg.util.editor2d.entity.Input;
import owg.util.editor2d.entity.Duplex;
import owg.util.editor2d.entity.Output;
import owg.util.editor2d.entity.Placeable2D;
import owg.util.editor2d.entity.Rotateable2D;
import owg.util.editor2d.entity.Scaleable2D;
import owg.util.euclidian.MatrixStack;
import owg.util.euclidian.V2D;
import owg.util.opengl.TextureLib;

public class Mogen extends EntityBase implements Placeable2D, Scaleable2D, Rotateable2D, PFieldOwner<Mogen>, PFieldElement {
	
	private String name;
	private V2D pos, speed;
	private float xScale, yScale;
	
	@Input
	public final PFieldOptional<Mogen> previous = new PFieldOptional<>(this, Mogen.class);
	@Duplex
	public final PFieldOptional<Mogen> mate = new PFieldOptional<>(this, Mogen.class);
	@Output
	public final PFieldOptional<Mogen> next = new PFieldOptional<>(this, Mogen.class);
	
	private float angle;
	
	public Mogen(InstanceList list, V2D pos, float xScale, float yScale) {
		super(list, 0);
		this.pos = pos;
		speed = new V2D();
		this.xScale = xScale;
		this.yScale = yScale;
	}
	public void pFieldChange(PFieldImmutable<? extends Mogen> field, Mogen newChild, int index)
	{
		if(newChild != null && newChild.getX() < getX())
			throw new IllegalArgumentException("CAN'T LET YOU DO THAT STAIRFAX");
	}
	
	@Override
	public float getAngle() {
		return angle;
	}
	
	@Override
	public void setAngle(float newAngle, boolean tentative) {
		angle = newAngle;
	}
	
	@Override
	public void setScale(float newXScale, float newYScale, boolean tentative) {
		xScale = newXScale;
		yScale = newYScale;
	}
	
	@Override
	public float getXScale() {
		return xScale;
	}
	
	@Override
	public float getYScale() {
		return yScale;
	}
	
	@Override
	public void setLocation(double x, double y, boolean tentative) {
		pos.set(x, y);
	}
	
	@Override
	public double getX() {
		return pos.x();
	}
	
	@Override
	public double getY() {
		return pos.y();
	}
	
	@Override
	public String getDefaultName() {
		return getClass().getSimpleName();
	}
	@Override
	public String toString() {
		return getName();
	}
	
	@Override
	public String getName() {
		return name;
	}
	
	@Override
	public void setName(String newName) {
		name = newName;
	}
	
	@Override
	public void beginStep() {
	}
	
	@Override
	public void step() {
		pos.add(speed);
	}
	
	@Override
	public void endStep() {
	}
	
	@Override
	public void render() {
		render(false);
	}
	@Override
	public void render(boolean shadeless) {
		MatrixStack m = egl.modelviewMatrix();
		m.push();
		if(!shadeless)
			egl.enableTexture2D(TextureLib.tex("editorCursor"), 0);
		m.translatef((float)pos.x(), (float)pos.y(), 0.0f);
		m.rotatef(angle, 0, 0, -1);
		m.scalef(xScale, yScale, 1);
		egl.unitSquare.render();
		egl.disableTexture2D();
		m.pop();
	}
}
